﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using BLL.MailSend;
using common;
using Model;
using WeiBoBLL;

namespace MailSendMore
{
    public partial class MainForm : Form
    {
        public delegate void DisplayUILog(string message);
        public delegate void OutDelegate();

        private static int isautorun = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["isautorun"]);

        public MainForm()
        {
            InitializeComponent();
            this.label4.Text = "0";
            this.label5.Text = "0";
            this.label6.Text = "0";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.nowTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string tempstr = SmtpOperation.getQueueState();
            string[] temp = tempstr.Split('$');
            this.label5.Text = temp[0].ToString();
            this.label4.Text = temp[1].ToString();
            this.label6.Text = temp[2].ToString();
        }

        //日志记录
        private void DisplayLog(string message)
        {
            if (mailLog.InvokeRequired)
            {
                Invoke(new DisplayUILog(DisplayLog), new object[] { message });
                return;
            }
            if (mailLog.Items.Count > 500)
            {
                mailLog.Items.Clear();
            }
            string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  " + message;
            mailLog.Items.Insert(0, msg);
            CommMethod.insertText(msg);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.startTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.nowTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            SmtpOperation smtp = SmtpOperation.getInstace();
            smtp.PutOutInfomation += new D_CallBack(testback);

            if (isautorun == 1)
            {
                SmtpOperation service = SmtpOperation.getInstace();
                SmtpOperation.readThreadFlag = false;
                SmtpOperation.writeThreadFlag = false;
                service.Run();
                if (service.IsRun)
                {
                    this.label8.Text = "运行中";
                }
                else
                {
                    this.label8.Text = "暂停！";
                }
            }
        }

        //邮件群发开
        private void btn_mailon_Click(object sender, EventArgs e)
        {
            try
            {
                SmtpOperation service = SmtpOperation.getInstace();
                SmtpOperation.readThreadFlag = false;
                SmtpOperation.writeThreadFlag = false;
                service.Run();
                if (service.IsRun)
                {
                    this.label8.Text = "运行中";
                }
                else
                {
                    this.label8.Text = "暂停！";
                }
            }
            catch (Exception ex)
            {
                DisplayLog(ex.Message);
            }
        }

        //邮件群发关
        private void tbn_mailoff_Click(object sender, EventArgs e)
        {
            SmtpOperation.readThreadFlag = true;
            SmtpOperation.writeThreadFlag = true;
            SmtpOperation smtp = SmtpOperation.getInstace();
            smtp.IsRun = false;
            if (smtp.IsRun)
            {
                this.label8.Text = "运行中";
            }
            else
            {
                this.label8.Text = "暂停！";
            }
            GC.Collect();
        }

        #region 退出程序处理
        //点击关闭按钮 处理队列的消息.
        private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            AppExit();
        }

        private void AppExit()
        {
            this.Hide();
            this.timer1.Enabled = false;
            try
            {
                SmtpOperation.setThreadFlag();

                //等邮件发送完关闭程序
                while (true)
                {
                    string tempstr = SmtpOperation.getQueueState();
                    string[] temp = tempstr.Split('$');
                    if (temp[0] == "0")
                    {
                        Thread.Sleep(100);
                        Application.Exit();
                        return;
                    }
                    Thread.Sleep(1000);
                }
            }
            catch
            {
                Application.Exit();
            }
        }
        #endregion

        public void testback(object sender, MyEventArgs e)
        {
            DisplayLog(e.Object_I.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WeiboModel mod = new WeiboModel();
            WeiBoBase wb = new WeiBoBase(mod);
            wb.Run();
        }
    }
}
